package cn.javabb.core.service.biz;

import cn.javabb.core.annotation.RedisCache;
import cn.javabb.core.base.BaseService;
import cn.javabb.core.constant.BizConstant;
import cn.javabb.core.entity.biz.CommentDO;
import cn.javabb.core.mapper.biz.CommentMapper;
import cn.javabb.core.query.CommentQuery;
import cn.javabb.core.service.sys.SysConfigService;
import cn.javabb.core.util.ShiroUtil;
import cn.javabb.core.vo.CommentArticleVO;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author QINB imqinbao@163.com
 * @CreateDate 2019/1/3/003 10:56
 * @Since V1.0
 */
@Service
public class CommentService extends BaseService<CommentDO> {

    @Autowired
    SysConfigService configService;
    @Autowired
    public CommentMapper commentMapper;

    public List<CommentDO> listComments(JSONObject param) {
        param.put(this.SQLID, CommentMapper.class.getName() + ".listCommentsByArticleId");
        return this.queryList(param);
    }

    /**
     * 查看帖子，获取每个帖子下面的评论
     * @param postId
     * @return
     */
    @RedisCache
    public List<CommentDO> listCommentsByPostId(Integer postId){
        return commentMapper.listCommentsByPostId(postId);
    }
    @RedisCache
    public List<CommentDO> listCommentsByBlogId(Integer blogId){
        return commentMapper.listCommentsByBlogId(blogId);
    }

    /**
     * 查看是否评论
     * @param userId
     * @param postId
     * @return
     */
    @RedisCache
    public boolean hasPostComment(Integer userId,Integer postId){
        CommentDO commentDO = new CommentDO();
        commentDO.setUserId(userId)
                .setArticleId(postId)
                .setArticleType(BizConstant.ARTICLE_TYPE_POST)
                .setCommentApproved(1);
        if(this.queryCountByWhere(commentDO)>0){
            return true;
        }
        return false;
    }

    /**
     * 保存评论，保存评论之前要看是否支持评论
     * @param commentDO
     * @return
     */
    @RedisCache(flush = true)
    public String saveComment(CommentDO commentDO){
        if(null == commentDO.getCommentApproved()){
            // 查看是否需要审核
            String commentReview = configService.getConfigValue(BizConstant.CONFIG_COMMENT_REVIEW);
            commentDO.setCommentApproved(
                    StringUtils.isNotBlank(commentReview)&&BizConstant.COMMENT_REVIEW_YEW.equals(commentReview)
                            ?0:1);
        }
        return this.insertOrUpdateSelective(commentDO);
    }
    /*帖子评论*/
    public Page listPostComments(CommentQuery query) {
        Page p = PageHelper.startPage(query.getPage(), query.getSize());
        List<CommentArticleVO> list = commentMapper.listPostComments(query);
        return p;
    }
    /*博客评论*/
    public Page listBlogComments(CommentQuery query) {
        Page p = PageHelper.startPage(query.getPage(), query.getSize());
        List<CommentArticleVO> list = commentMapper.listBlogComments(query);
        return p;
    }
    /**
     * 根据文章ID删除评论
     * @param articleId
     * @param articleType
     * @return
     */
    public boolean deleteByArticleId(Integer articleId,Integer articleType) {
        CommentDO commnet = new CommentDO().setArticleId(articleId).setArticleType(articleType);
        return this.deleteByWhere(commnet)>0;
    }

    /*判断记录用户一致*/
    public boolean judgeSameUser(int commentId) {
        CommentDO comment = this.queryById(commentId);
        if (ShiroUtil.getLoginUser().getId() .equals(comment.getUserId()) ) {
            return true;
        }
        return false;
    }

}
